package com.yeyaomai.dksns.flowis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import com.yeyaomai.dksns.flowis.dao.AccessAnalysisDao;
import com.yeyaomai.dksns.flowis.dao.DetailDao;
import com.yeyaomai.dksns.flowis.domain.AccessAnalysis;
import com.yeyaomai.dksns.flowis.domain.AccessDate;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisQueryOrder;
import com.yeyaomai.dksns.flowis.page.PageImplResult;
import com.yeyaomai.dksns.flowis.page.PageRequestOrder;
import com.yeyaomai.dksns.flowis.page.SplitConstant;
import com.yeyaomai.dksns.flowis.service.AccessAnalysisService;
import com.yeyaomai.dksns.flowis.util.CollectionNames;

/**
 * 搜索引擎Service层实现
 * @author sun_shan
 * 
 */
@Service
public class AccessAnalysisServiceImpl implements AccessAnalysisService {

	@Resource
	private AccessAnalysisDao accessAnalysisDao ;
	
	@Resource
	private DetailDao detailDao;

	@Override
	public void addAccessAnalysis(AccessAnalysis access) {
		
		accessAnalysisDao.addAccessAnalysis(access);
	}
	
	@Override
	public List<AccessAnalysis> getAccessAnalysisByDate(String st, String et, String page, String url) {
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("st", st);
		map.put("et", et);
		map.put("url", url);
		if(page.equals("enter"))  return  accessAnalysisDao.getEnterByDate(map);
		else if(page.equals("out"))  return accessAnalysisDao.getOutDate(map); 
		else return accessAnalysisDao.getBrowseByDate(map); 
	}
	

	@Override
	public Page<AccessAnalysis> getSplit(FlowisQueryOrder order) {
		
		PageRequestOrder page = new PageRequestOrder(Integer.valueOf(order.getNumber()) , SplitConstant.PAGE_SIZE);
		List<AccessAnalysis> accessList = getAccessAnalysisByDate(order.getStartTime(), order.getEndTime(), order.getPage(), order.getUrl());
		List<AccessAnalysis> aaList =	interception(accessList, page.start() ,page.end());
		Page<AccessAnalysis> result = new PageImplResult<AccessAnalysis>(aaList, page, accessList.size());
		return result;
	}
	
	public List<AccessAnalysis> interception(List<AccessAnalysis> accessList ,Integer start , Integer ent){
		
		List<AccessAnalysis> list = new ArrayList<AccessAnalysis>();
		if(start>=0){
			ent =  ent>accessList.size() ? accessList.size() : ent ;
			for(int i = start; i< ent; i++ ){
				list.add(accessList.get(i));
			}
		}
		return list;
	}
	
	@Override
	public GroupByResults<AccessDate> getAccessAnalysis(String st, String et) {
		
		String mapFunction = "{browseNumber : 0 }";
		String reduceFunction = "function(key,values){values.access_url, values.browseNumber +=1 }";
		
		Criteria criteria = Criteria.where("access_date").gte(st).lt(et);
		return detailDao.group(criteria, CollectionNames.ACCESS_URL,
				mapFunction, reduceFunction, AccessDate.class);
	}
}
